<?php
/**
 * OAuth_Request
 * Use this class to form an OAuth request. This class is passed to the
 * OAuth_Dispatcher which performs the actual request.
 * 
 * @package OAuth
 * @copyright Luke Visinoni (luke.visinoni@gmail.com)
 * @author Luke Visinoni (luke.visinoni@gmail.com)
 * @license GNU Lesser General Public License
 */
class OAuth_Request {

    /**
     * @var OAuth_Uri The URI that is being requested
     */
    protected $requestUri;
    
    /**
     * @var string The method used for this request
     */
    protected $requestMethod;
    
    /**
     * @var array An array of query parameters for the request
     */
    protected $queryParams = array();
    
    /**
     * @var array An array of post parameters for the request
     */
    protected $postParams = array();
    
    /**
     * @var array The request headers
     */
    protected $headers = array();
    
    /**
     * Class Constructor
     * @access public
     * @param string The Uri that is being requested
     */
    public function __construct($uri = null, $method = 'POST') {
    
        $this->setRequestUri($uri)
            ->setRequestMethod($method);
    
    }
        
    /**
     * Get the URI that is being requested
     * @access public
     * @param string The Uri that is being requested
     */
    public function getRequestUri() {
    
        return $this->requestUri;
    
    }
    
    /**
     * Set the URI that is being requested
     * @access public
     * @param string The Uri that is being requested
     */
    public function setRequestUri($uri) {
    
        $this->requestUri = OAuth_Uri::factory($uri);
        return $this;
    
    }
        
    /**
     * Get the URI that is being requested
     * @access public
     * @param string The Uri that is being requested
     */
    public function getRequestMethod() {
    
        return $this->requestMethod;
    
    }
    
    /**
     * Set the URI that is being requested
     * @access public
     * @param string The Uri that is being requested
     */
    public function setRequestMethod($method) {
    
        $this->requestMethod = $method;
        return $this;
    
    }
    
    /**
     * Sets a GET parameter for the request
     */
    public function setQueryParam($name, $value) {
    
        $this->requestUri->setParam($name, $value);
        return $this;
    
    }
    
    /**
     * Gets a GET parameter from the request
     */
    public function getQueryParam($name) {
    
        return $this->requestUri->getParam($name);
    
    }
    
    /**
     * Sets a POST parameter for the request
     */
    public function setPostParam($name, $value) {
    
        $this->postParams[$name] = $value;
        return $this;
    
    }
    
    /**
     * Gets a POST parameter from the request
     */
    public function getPostParam($name) {
    
        return $this->postParams[$name];
    
    }
    
    /**
     * Sets a header parameter for the request
     */
    public function setHeader($name, $value) {
    
	$name = $this->normalizeHeaderName($name);
        $this->headers[$name] = trim($value);
        return $this;
    
    }
    
    /**
     * Adds a header parameter for the request (doesn't overwrite previous)
     */
    public function addHeader($name, $value) {
    
        $name = $this->normalizeHeaderName($name);
        if ($this->hasHeader($name)) {
            
        }
        return $this;
    
    }
    
    public function hasHeader($name) {
    
	$name = $this->normalizeHeaderName($name);
	return array_key_exists($name, $this->headers);
    
    }
    
    /**
     * Gets a header parameter from the request
     */
    public function getHeader($name) {
    
	$name = $this->normalizeHeaderName($name);
        return $this->hasHeader($name) ?
	    $this->headers[$name] :
	    null ; // @todo should this just return null like this? Seems sloppy.
    
    }
	
    protected function normalizeHeaderName($name) {
    
	$name = strtolower(trim($name));
	return $name;
    
    }

}
